package org.jackysoft.interceptor;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jackysoft.annotations.ActionRequestMethod;
import org.jackysoft.annotations.MethodInceptor;
import org.jackysoft.query.RunnerQuery;
import org.springframework.web.servlet.ModelAndView;

@MethodInceptor(method = ActionRequestMethod.query)
public class QueryInterceptor extends AbstractInterceptor {

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		int page = (int) modelAndView.getModelMap().get("page");
		int rows = (int) modelAndView.getModelMap().get("rows");
		RunnerQuery q = (RunnerQuery) modelAndView.getModelMap().get("query");
		Method service_m_gets = service.getClass().getMethod("query",
				RunnerQuery.class, int.class, int.class);

		// 从数据库查询得到的对象
		Object pageBean = service_m_gets.invoke(service, q, page, rows);

		modelAndView.addObject("pager", pageBean);
	}

}
